package com.it.data.manager

import android.app.Application
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import android.provider.Settings.Secure
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import java.util.UUID


/**
 *  @Description：
 *  @author：weishixiong
 *  @date：2023/4/3 9:54
 *
 */
object AppManager {
    lateinit var appContext: Application
    var resumeActivity: AppCompatActivity? = null;
    const val TAG = "AppManager"
    var FILE_PATH = Environment.getExternalStorageDirectory().toString() + "/hm/" //日志根路径


    fun getAppVersionCode(context: Context): Int {
        var versionCode = 0
        try {
            val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
            versionCode = pInfo.versionCode
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return versionCode
    }


    fun getAppVersionName(context: Context): String {

        var versionName = ""
        try {
            val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
            versionName = pInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return versionName
    }

    private val versionMap = mapOf(
        "31" to "Android 12.0",
        "30" to "Android 11.0",
        "29" to "Android 10.0",
        "28" to "Android 9.0",
        "27" to "Android 8.1",
        "26" to "Android 8.0",
        "25" to "Android 7.1.1",
        "24" to "Android 7.0",
        "23" to "Android 6.0",
        "22" to "Android 5.1",
        "21" to "Android 5.0",
        "20" to "Android 4.4W",
        "19" to "Android 4.4",
        "18" to "Android 4.3",
        "17" to "Android 4.2",
        "16" to "Android 4.1",
        "15" to "Android 4.0.3",
        "14" to "Android 4.0"

    )

    fun getSystemVersion(): String? = versionMap[Build.VERSION.SDK]

    fun getDeviceCode(context: Context): String {
        var serial = ""
        try {
            val c = Class.forName("android.os.SystemProperties")
            val get = c.getMethod("get", String::class.java)
            serial = get.invoke(c, "ro.serialno") as String

            if (!TextUtils.isEmpty(serial) && !serial.equals("unknown", ignoreCase = true)) {
                return serial
            }
        } catch (e: java.lang.Exception) {
            serial = ""
        }
        return "0020202409017087"
    }

    private fun getUUID(): String? {
        var serial: String? = null
        val m_szDevIDShort =
            "35" + Build.BOARD.length % 10 + Build.BRAND.length % 10 + (if (null != Build.CPU_ABI) Build.CPU_ABI.length else 0) % 10 + Build.DEVICE.length % 10 + Build.DISPLAY.length % 10 + Build.HOST.length % 10 + Build.ID.length % 10 + Build.MANUFACTURER.length % 10 + Build.MODEL.length % 10 + Build.PRODUCT.length % 10 + Build.TAGS.length % 10 + Build.TYPE.length % 10 + Build.USER.length % 10 //13 位
        if (Build.VERSION.SDK_INT <= 29) {
            try {
                serial = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    Build.getSerial()
                } else {
                    Build.SERIAL
                }
                //API>=9 使用serial号
                return UUID(
                    m_szDevIDShort.hashCode().toLong(),
                    serial.hashCode().toLong()
                ).toString()
            } catch (exception: java.lang.Exception) {
                serial = "serial" // 随便一个初始化
            }
        } else {
            serial = Build.UNKNOWN // 随便一个初始化
        }

        //使用硬件信息拼凑出来的15位号码
        return UUID(m_szDevIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
    }


    fun getAndroidId(context: Context): String {
        return Secure.getString(context.contentResolver, Secure.ANDROID_ID)
    }

}